﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Game2048WithCSharp
{
    internal class Cube: Label
    {
        static Dictionary<int, Color> colors = new Dictionary<int, Color>()
        {
            { 2, Color.FromArgb(159, 222, 212) },
            { 4, Color.FromArgb(145, 222, 208) },
            { 8, Color.FromArgb(131, 221, 204) },
            { 16, Color.FromArgb(117, 221, 200) },
            { 32, Color.FromArgb(103, 220, 196) },
            { 64, Color.FromArgb(88, 219, 192) },
            { 128, Color.FromArgb(74, 219, 188) },
            { 256, Color.FromArgb(60, 218, 184) },
            { 512, Color.FromArgb(46, 218, 180) },
            { 1024, Color.FromArgb(32, 217, 176) },
            { 2048, Color.FromArgb(18, 217, 172) }
        };
        private int value;
        private int oldx, oldy;
        public int x, y;
        public double TaskTotalTime = 0;
        private double NowTime;
        public bool TaskComplete = true;
        public int Value { 
            get { return value; } 
            set 
            {
                this.value = value;
                BackColor = colors[value];
                Text = value.ToString();
            }
        }
        public bool Equal(Cube cube)
        {
            return x == cube.x && y == cube.y;
        }
        public Cube(int value, int x, int y)
        {
            TextAlign = ContentAlignment.MiddleCenter;
            this.Font = new Font("宋体", 15, FontStyle.Bold);
            AutoSize = false;
            this.x = x;
            this.y = y;
            Location = Index2Pos(x, y);
            this.Size = MagicConstUI.CubeSize;
            Value = value;
        }
        public void StartMove(int x, int y)
        {
            oldx = this.x; oldy = this.y;
            this.x = x; this.y = y;
            TaskTotalTime = (Math.Abs(oldx - x) + Math.Abs(oldy - y)) * MagicConstUI.SingleMoveTime;
            NowTime = 0;
            TaskComplete = false;
        }

        public bool UpdateMove(double DeltaTime)
        {
            if (!TaskComplete)
            {
                NowTime += DeltaTime;
                if (NowTime > TaskTotalTime)
                {
                    NowTime = TaskTotalTime;
                    TaskComplete = true;
                }
                double nextx = oldx + (x - oldx) * NowTime / TaskTotalTime;
                double nexty = oldy + (y - oldy) * NowTime / TaskTotalTime;
                Location = Index2Pos(nextx, nexty);
            }
            return TaskComplete;
        }
        private Point Index2Pos(double x, double y)
        {
            int y_pos = (int)(MagicConstUI.StartPos.Y + x * (MagicConstUI.CubeSize.Width + MagicConstUI.Padding.Width) + MagicConstUI.Padding.Width);
            int x_pos = (int)(MagicConstUI.StartPos.X + y * (MagicConstUI.CubeSize.Height + MagicConstUI.Padding.Height) + MagicConstUI.Padding.Height);
            return new Point(x_pos, y_pos);
        }
    }
}
